package application.presenter;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;

/** @author Krystian */

/** Klasa wizualizuje wierzcholek w grafie */

public class VisualNode {

	private static double r;

	private double angle;

	private int id;

	private double x, y;
	private Color color;
	private ArrayList<Color> possibleColors;

	private double dim[] = new double[4];

	/** Konstruktor */
	public VisualNode(int id, double x, double y, String colorHex) {

		this.id = id;

		this.x = x;
		this.y = y;

		this.dim[0] = x - r;
		this.dim[1] = y - r;
		this.dim[2] = 2 * r;
		this.dim[3] = 2 * r;

		setColor(colorHex);
	}

	/** Konstruktor */
	public VisualNode(int id, double x, double y, String colorHex,
			double angle, ArrayList<String> possibleColors) {

		this.id = id;

		this.possibleColors = new ArrayList<Color>();

		for (String s : possibleColors) {
			this.possibleColors.add(setColor(s));
		}

		this.x = x;
		this.y = y;

		this.angle = angle;

		this.dim[0] = x - r;
		this.dim[1] = y - r;
		this.dim[2] = 2 * r;
		this.dim[3] = 2 * r;

		color = setColor(colorHex);
	}

	/** Zwraca X */
	public double getX() {
		return x;
	}

	/** Zwraca Y */
	public double getY() {
		return y;
	}

	/** Ustawienie statycznego promienia */
	public static void setR(float newR) {
		r = newR;
	}

	/** Ustawianie koloru wierzcholka */
	public Color setColor(String colorHex) {
		try {
			return Color.decode("#" + colorHex);
		} catch (Exception e) {
			System.out.println("Źle zdefiniowany kolor");
			throw e;
		}
	}

	/** Maluje wierzcholek */
	public void draw(Graphics2D g) {
		g.setColor(Color.black);
		g.drawString(String.valueOf(id), (int) (dim[0] + dim[2]), (int) dim[1]);
		g.setColor(color);
		g.fill(new Ellipse2D.Double(dim[0], dim[1], dim[2], dim[3]));

		if (possibleColors != null) {
			int dist = 32;
			for (Color c : possibleColors) {
				g.setColor(c);
				g.fill(new Ellipse2D.Double(x + dist * Math.sin(angle) - 3, y
						+ dist * Math.cos(angle) - 3, 6, 6));
				dist += 8;
			}
		}

	}
}
